package com.example.guiguihomemedical.service.impl;

import com.example.guiguihomemedical.entity.dto.RecordList;
import com.example.guiguihomemedical.entity.dto.RecordSearch;
import com.example.guiguihomemedical.service.RecordListService;
import com.example.guiguihomemedical.service.RecordSearchService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RecordSearchServiceImpl implements RecordSearchService {
    @Autowired
    private RecordListService recordListService;

    @Override
    public List<RecordList> searchRecord(RecordSearch recordSearch) {
        List<RecordList> recordLists = recordListService.getRecordList();
        List<RecordList> currentRecordList = new ArrayList<>();
        for (RecordList recordList : recordLists) {
            if (matchesSearchCriteria(recordList, recordSearch)) {
                currentRecordList.add(recordList);
            }
        }
        Integer recordSearchCount=currentRecordList.size();
        for(RecordList recordList:currentRecordList){
            recordList.setRecordSearchCount(recordSearchCount);
        }
        return currentRecordList;
    }

    private boolean matchesSearchCriteria(RecordList record, RecordSearch recordSearch) {
        // 检查签约状态是否符合条件
        if (record.getStatus() != recordSearch.getStatus() && recordSearch.getStatus() != -1) {
            log.info("签约状态不等");
            return false;
        }
        // 检查性别是否符合条件
        if (!record.getSex().equals(recordSearch.getSex()) && !recordSearch.getSex().equals("-1")) {

            return false;
        }
        // 检查年龄是否符合条件
        if (record.getAge() != recordSearch.getAge() && recordSearch.getAge() != -1) {
            return false;
        }
        if (!(record.getIdcard().contains(recordSearch.getIdNamePhone()) ||
                record.getName().contains(recordSearch.getIdNamePhone()) ||
                record.getPhone().equals(recordSearch.getIdNamePhone())) &&
                !recordSearch.getIdNamePhone().equals("-1")) {
            log.info(record.getPhone()+"idcart,name,phone不等"+recordSearch.getIdNamePhone());
            return false;
        }
        // 人群种类
        if (recordSearch.getListenername() != null &&  !record.getListenername().containsAll(recordSearch.getListenername())) {
            log.info("人群种类不等");
            return false;
        }
        // 如果所有条件都匹配，则返回 true
        return true;
    }
}
